<?php

namespace app\api\model;

use think\Cache;
use think\Exception;
use think\Model;
use think\Db;

class Orders extends Model
{

    protected $dateFormat = 'Y-m-d H:i:s';
    protected $createTime = "ctime";
    protected $updateTime = "utime";

   

    /**
     * @param $where
     * @param int $page
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenHong~
     * 查询当前用户的订单列表
     */
    public function getByUserIdSelectOrder($where, $page = 1,$listnum=10)
    {

        $list['list'] = $this
                ->where($where)
                ->page($page, $listnum)
                ->order('ctime desc')
                ->select();

        $list['total'] = $this->where($where)->count();
        return $list;
    }



    /**
     * @param $orderNo
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenHong~
     * 根据订单号获取相关订单信息
     */
    protected function getOrderInfo($orderNo)
    {

        $order = self::get(['ordernumber' => $orderNo])->getData();

        $join = [
            ['__ATTESTATION__ A', 'FIND_IN_SET(A.id, C.attestation_id)', 'LEFT']
        ];

        //   查询商户认证信息、店面名称、详细地址、商户图片
        $cashInfo = Db::name('Cash')->alias('C')
            ->join($join)
            ->field('C.hour_free,C.name,C.detailed_address,C.cover_nowprice,C.getcar_nowprice,C.get_car,C.pic,GROUP_CONCAT(A. `NAME`) att_name')
            ->where('C.a_id', $order['a_id'])
            ->group('C.a_id')
            ->find();

        //   设置星期数组
        $week = array("周日", "周一", "周二", "周三", "周四", "周五", "周六");

        //   获取下单日期是星期几
        $myWeek = $week[date('w', strtotime($order['reservedate']))];

        //  获取用户车辆信息
        $userCar = Db::name('Carinfor')->where('c_id', $order['car_id'])->field('b_name,t_name,m_name,licenseplate')->find();

        //  查询该订单ID 是否选择了上门取车
        $isCarOrder = Db::name('Getcarorder')->where('order_id', $order['id'])->value('id');

        //   传递订单页面的数据(订单详情与下单页数据共用)
        $arr = [
            'cash_pic' => $cashInfo['pic'],
            'cash_name' => $cashInfo['name'],
            'attr_name' => $cashInfo['att_name'],
            'detailed_address' => $cashInfo['detailed_address'],
            'hour_free' => $cashInfo['hour_free'],
            'cover_nowprice' => $cashInfo['cover_nowprice'],
            'getcar_nowprice' => $cashInfo['getcar_nowprice'] ?: 0,
            'get_car' => $cashInfo['get_car'],
            'ordernumber' => $order['ordernumber'],
            'machineoil' => $order['machineoil'],
            'product' => $order['product'],
            'goTime' => $order['reservedate'] . ' (' . $myWeek . ') ' . $order['reservetime'],
            'pay_price' => $order['pay_price'],
            'user_name' => $order['name'],
            'user_phone' => $order['phone'],
            'order_status' => getOrderStatus($order['status']),
            'order_time' => date('Y-m-d H:i:s', $order['ctime']),
            'user_car' => $userCar['b_name'] . ' ' . $userCar['t_name'] . ' ' . $userCar['m_name'],
            'user_car_number' => $userCar['licenseplate'],
            'is_car_order' => $isCarOrder ? 'ok' : 'no',
            'order_type' => $order['type']
        ];

        return $arr;
    }

    /**
     * @param $where
     * @param $page
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenhong~
     * 查询订单列表
     */
    public function selectOrder($where, $page,$listnum=10)
    {

        // $list['list'] = self::select(function ($query) use ($where, $page) {
        //     $query->where($where)->page($page, 10)->order('ctime desc');
        // });
        // $list['total'] = self::where($where)->count();

        $list['list'] = $this
            ->where($where)
            ->page($page, $listnum)
            ->order('ctime desc')
            ->select();
            file_put_contents('sql.txt', $this->getLastSql());
        $list['total'] = $this->where($where)->count();

        return $list;
    }

    /*
    * Notes: 金额统计
    * Create Date: 2018/10/16
    * Create Time: 下午3:50
    * Create by :张福来
    * param :
    */
    public function orderMoney($wh, $group)
    {
        $sql = $this
            ->field("pay_price,status,ctime,a_id,FROM_UNIXTIME(ctime,'%Y-%m-%d %H:%i:%s') my_date,FROM_UNIXTIME(ctime,$group) group_time")
            ->buildSql();
        $result["data"] = Db::table("{$sql}")
            ->alias("a")
            ->field("a.pay_price price,SUM(a.pay_price) totalprice,a.group_time")
            ->where($wh)
            ->group("a.group_time")
            ->select();
        $result["alltotal"] = $this
            ->alias("a")
            ->field("SUM(a.pay_price) alltotal")
            ->where($wh)
            ->find();
        return $result;
    }
}